package work;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Training {
	HashMap<String,String> hashTable = new HashMap<String,String>();
	public int readTrainingFile(String filename) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename),"gb2312"));
		String str = in.readLine();
		while(str != null){
			byte[] str_bytes = str.getBytes("UTF-8");
			String str_utf = new String(str_bytes, 0, str_bytes.length, "UTF-8");
			int pos = parse(str, 0);
			while(pos > -1){
				pos = parse(str_utf, pos);
			}
			str = in.readLine();
		}
		in.close();
		return hashTable.size();
	}
	private int parse(String str, int pos){
		int start = str.indexOf("{", pos);
		int mid = str.indexOf("/",pos);
		int end = str.indexOf("}",pos);
		if(start != -1 && mid != -1 && end != -1 && start < mid && mid < end){
			String value = str.substring(start + 1, mid);
			String attr = str.substring(mid + 1, end);
			hashTable.put(value, attr);
			return end + 1;
		}
		return -1;
	}
	public void printTrainingResult(){
		int linelength = 80;
		for(String value: hashTable.keySet()){
			linelength -= (value.length() + 1);
			if(linelength < 0){
				System.out.println();
				linelength = 80;
			}
			System.out.print(value + " ");
			
		}
		System.out.println("\nTotal : " + hashTable.size());
	}
}
